package com.buaa.utility.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

public class StringUtil {
	public static boolean isEmpty(String s){
		return s==null || "".equals(s);
	}

	public static String getString(String str){
		if(str==null){
			return "";
		}else{
			return str;
		}
	}
	
	public static String getString(String str,String defaultStr){
		if(isEmpty(str))
			return defaultStr;
		else
			return str;
	}
	
	public static String getValueByParams(String str,Map map){
		if(map!=null){
			if(str==null){
				return "";
			}else{
				if(map.get(str)==null){
					return "";
				}else{
					return map.get(str).toString();
				}
			}
		}else{
			return "";
		}
	}
	
	public static String formatString(String string){
		DecimalFormat df = new DecimalFormat();
		df.applyPattern("###,##0.##");
		String str;
		if(string==null){
			str = "";
		}else if(string.equals("--")){
			str = string;
		}else if(string.equals("NaN")){
			str = "";
		}else{
			str = df.format(Double.valueOf(string));
		}
		return str;
	}
	
	public static String getUnicode(String str) {
		if (str == null) {
			return null;
		}
		try {
			return new String(str.getBytes("gb2312"), "iso-8859-1");
		} catch (Exception ex) {
			return str;
		}
	}
	
	public static String getYearMoth() {
		Date da = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dd = sdf.format(da);
		String times[] = dd.split("-");
		String yearStr = times[0];
		String monthStr = times[1];
		String dayStr = times[2];
		int year = Integer.parseInt(yearStr);
		int month = Integer.parseInt(monthStr);
		int day = Integer.parseInt(dayStr);
		if (day < 5) {
			month = month - 2;
			if (month == 0) {
				month = 12;
				year = year - 1;
			}
			if (month == -1) {
				month = 11;
				year = year - 1;

			}
		} else {
			month = month - 1;

			if (month == 0) {
				month = 12;
				year = year - 1;
			}
		}

		if (month < 10) {
			monthStr = "0" + month;
		} else {
			monthStr = String.valueOf(month);
			yearStr = String.valueOf(year);
		}

		String yearMonthDay = yearStr + "-" + monthStr;
		System.out.println(yearMonthDay);
		return yearMonthDay;
	}
	
	public static String getYearMothDay()
	  {
	    Calendar calendar = Calendar.getInstance();
	    calendar.add(Calendar.DATE,-1);
	    Date d = calendar.getTime();
	    
	    SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
	    String yearMonthDay = sp.format(d);
	    return yearMonthDay;
	  }
	
	public static void main(String[] args) {
		System.out.println(StringUtil.getYearMoth());
	}
    
    public static String getPreviousWeekSunday()
    {
      int weeks = 0;
      weeks--;
      int mondayPlus = getMondayPlus();
      GregorianCalendar currentDate = new GregorianCalendar();
      currentDate.add(5, mondayPlus + weeks);
      Date monday = currentDate.getTime();
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      String preMonday = df.format(monday);
      return preMonday;
    }
   
    public static String getDateXun()
    {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Calendar cal = Calendar.getInstance();
      int day = cal.get(5);
      if (day <= 10)
        return getPreviousMonthEnd();
      if (day <= 20)
        cal.set(5, 10);
      else if (day > 20) {
        cal.set(5, 20);
      }
      String dateXun = sdf.format(cal.getTime());
      return dateXun;
    }
    
    private static int getMondayPlus() {   
         Calendar cd = Calendar.getInstance();      
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1; 
        if (dayOfWeek == 1) {   
            return 0;   
         } else {   
            return 1 - dayOfWeek;   
         }   
     } 
    public static String getPreviousMonthEnd(){
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
    	Calendar lastDate = Calendar.getInstance();   
        lastDate.set(Calendar.DATE,1);
    	lastDate.add(Calendar.DATE,-1);
    	String str=sdf.format(lastDate.getTime());   
    	return str;
    }

    
    
}
